#pragma once
#include "BasicPlaySound.h"
#include <Mmsystem.h>	// In windows.h
#pragma comment(lib, "Winmm.lib")

namespace utility
{
	BasicPlaySound::BasicPlaySound(void)
	{
		m_dwCurPos		= 0;
		m_dwSoundLength	= 0;
		m_nStatus		= 0;
		m_wDeviceID		= 0;
	}

	BasicPlaySound::~BasicPlaySound(void)
	{
	}
	bool BasicPlaySound::OpenFile( string_t strFilePath )
	{
		int nTryCount = 0;
		while( nTryCount++ < 2 )
		{
			MCI_OPEN_PARMS OpenParams;
			memset( &OpenParams, 0x00, sizeof(MCI_OPEN_PARMS)); 
			OpenParams.lpstrElementName	= strFilePath.c_str();
			if( mciSendCommand(NULL,MCI_OPEN, MCI_OPEN_ELEMENT,(DWORD_PTR)(LPVOID)&OpenParams ) == 0 ){
				m_wDeviceID=OpenParams.wDeviceID;
				return true;
			}
		}

		char_t lpszTemp[256] = {0};
		_stprintf_s(lpszTemp,256, _T("OpenFile Failed! LastError = %d\r\n"), GetLastError() );
		OutputDebugString( lpszTemp );
		return false;
	}
	bool BasicPlaySound::Play( bool bRePlay )
	{
		if( m_wDeviceID )
		{
			if( bRePlay )
				mciSendCommand(m_wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, NULL);

			MCI_SET_PARMS SetParams;
			SetParams.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
			mciSendCommand(m_wDeviceID,MCI_SET,MCI_SET_TIME_FORMAT,(DWORD_PTR)(LPVOID)&SetParams);

			//get pos.
			MCI_STATUS_PARMS StatusParams1;
			StatusParams1.dwItem = MCI_STATUS_POSITION;
			mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD_PTR)(LPVOID)&StatusParams1);
			m_dwCurPos = (DWORD)StatusParams1.dwReturn;

			//get length.
			MCI_STATUS_PARMS StatusParams2;
			StatusParams2.dwItem=MCI_STATUS_LENGTH;
			mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD_PTR)(LPVOID)&StatusParams2);
			m_dwSoundLength = (DWORD)StatusParams2.dwReturn;

			//play music.
			//mciPlayParms.dwCallback = (DWORD)pWnd->m_hWnd;
			//ON_MESSAGE(MM_MCINOTIFY, &CMyDlg::OnMCINotify)
			//LRESULT CMyDlg::OnMCINotify(WPARAM wParam, LPARAM lParam)  
			//{  
			//	 if (wParam == MCI_NOTIFY_SUCCESSFUL ){  
			//	     MCI_PLAY_PARMS PlayParms;
			//		 PlayParms.dwFrom = 0;
			//		 PlayParms.dwCallback = (DWORD)m_hWnd;
			//		 mciSendCommand(m_wDeviceID, MCI_SEEK,MCI_SEEK_TO_START, NULL);
			//		 mciSendCommand(m_wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)(LPVOID)&PlayParms)
			//	 }  
			//	 return 0;  
			//}

			MCI_PLAY_PARMS PlayParams;
			PlayParams.dwFrom = m_dwCurPos;
			if( mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_NOTIFY|MCI_FROM,(DWORD_PTR)(LPVOID)&PlayParams ) == 0 ){
				m_nStatus = 1;
				return true;
			}
			char_t lpszTemp[256] = {0};
			_stprintf_s(lpszTemp,256, _T("Play Sound Failed! LastError = %d\r\n"), GetLastError() );
			OutputDebugString( lpszTemp );
		}
		return false;
	}

	bool BasicPlaySound::PlayEx()
	{
		MCI_PLAY_PARMS pp;
		pp.dwCallback = NULL;
		pp.dwFrom = 0;
		pp.dwTo = 0;
		if( mciSendCommand(m_wDeviceID,MCI_PLAY,MCI_WAIT,(DWORD)&pp) == 0 )
		{
			return true;
		}
		return false;
	}

	bool BasicPlaySound::Pause()
	{
		mciSendCommand( m_wDeviceID, MCI_PAUSE, 0, NULL);
		m_nStatus = 2;
		return true;
	}
	bool BasicPlaySound::StopSound()
	{
		if( 0 == m_wDeviceID )
			return false;

		mciSendCommand( m_wDeviceID, MCI_STOP, 0, NULL);//MCI_WAIT
		m_nStatus = 4;
		return true;
	}
	bool BasicPlaySound::CloseSound()
	{
		if( 0 == m_wDeviceID )
			return false;

		mciSendCommand(m_wDeviceID,MCI_CLOSE,0,NULL);
		m_wDeviceID = 0;
		//CloseMIDIPlayer();
		m_nStatus = 0;
		return true;
	}
	void BasicPlaySound::UpdateCurPos()
	{
		MCI_STATUS_PARMS StatusParams;
		StatusParams.dwItem = MCI_STATUS_POSITION;
		if( mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD_PTR)(LPVOID)&StatusParams) == 0 ){
			m_dwCurPos = (DWORD)StatusParams.dwReturn;

			if( m_dwCurPos == m_dwSoundLength )
				m_nStatus = 3;
		}
	}
	int BasicPlaySound::GetPlayStatus()
	{
		return m_nStatus;
	}
}